<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/25
 * Time: 15:04
 */

namespace app\admin\behavior;

use app\common\model\System;
use org\Ip;
use think\Db;
use think\Request;

class apiwechat
{
    public function run(&$params)
    {
        if (empty(cookie('think_var'))) {
            cookie('think_var', 'zh-cn');
            $this->autolang();
        }
        $this->wechat();
        $this->allsystem();
    }

    public function autolang()
    {
        $request = Request::instance();
        $s = Db('system')->where('title', 'autolang')->value('value');
        cookie('autolang',$s);
        //开启自动选择语言模式
        if ($s=='on') {
//                    $taobaodata = implode(" ", Ip::find('159.187.2.10'));
//                    $taobaodata = implode(" ", Ip::find('14.215.176.133'));
            $taobaodata = implode(" ", Ip::find($request->ip()));

            $lang = array(
                '中国',
                '香港',
                '台湾',
                '保留地址',
                '本机地址',
                '局域网',
            );
            cookie('think_var', 'en-us');
            foreach ($lang as $k) {
                if (mb_strpos($taobaodata, $k) === false) {
                    continue;
                }
                cookie('think_var', 'zh-cn');
                break;
            }
        }
    }

    public function wechat()
    {

        $config = cache('wechat' . cookie('think_var'));
        if (empty($config)) {

            // 行为逻辑
            $result = (new system())->getnoteall('Wechat');
            if (empty($result)) {
                echo '数据库信息错误';
                cookie('think_var', null);
            }
            $config = [
                'token' => $result['Token']['value'],
                'appid' => $result['appid']['value'],
                'appsecret' => $result['appsecret']['value'],
                'EncodingAESKey' => $result['EncodingAESKey']['value'],
                //需要与腾讯商户后台API密钥保持一致
//                'key'=>$result['key']['value'],
//                //使用微信红包接口等功能时需要的证书,请登录微信支付后台下载
//                'apiclient_cert'=>$result['apiclient_cert']['value'],
//                'apiclient_key'=>$result['apiclient_key']['value'],
//                'rootca'=>$result['rootca']['value'],
//                //商户号
//                'mch_id'=>$result['mch_id']['value'],
//                //公众账号appid
//                'wxappid'=>$result['wxappid']['value'],

                'wxappid' => 'wxb4240fb263f12d9f',
                //商户号
                'mch_id' => '1503576721',

                'key' => 'qiantanglianou2018qiantang201811',

                'apiclient_cert' => $result['Token']['value'],
                'apiclient_key' => $result['Token']['value'],
                'rootca' => $result['Token']['value'],
            ];
            cache('system_Wechat' . cookie('think_var'), $result);

            cache('wechat' . cookie('think_var'), $config);
        }

        new \wechat\src\WeChat($config);
    }


    public function allsystem()
    {
        //全站信息
        $system = cache('systemsystem' . cookie('think_var'));
        if (empty($system)) {

            $system = (new system())->getnoteall('system');
            cache('systemsystem' . cookie('think_var'), $system);
        }

        //后台全站信息
        $system_admin = cache('systemsystem' . cookie('think_var_admin'));
        if (empty($system_admin)) {

            $system_admin = (new system())->getadminnoteall('system');
            cache('systemsystem' . cookie('think_var_admin'), $system_admin);
        }
    }


}